<?php defined('SYSPATH') or die('No direct script access.');

class Model_Services_Identitycard extends Model implements Jqgrid_GridListener{

	public $identitycard = NULL;

	public function __construct($id=false){
		if ($id) $this->identitycard = $this->get($id);
	}

	public function count($criteria = false){
		return ORM::factory("dao_identitycard")->count_all();
	}
		
	public function getList($criteria = false){
			
		if (!$criteria)	$identitycard_list = ORM::factory("dao_identitycard")->find_all()->as_array();
		else{
			if ($criteria->isSearch){
				$identitycard_list = ORM::factory("dao_identitycard")
								->where($criteria->getSQLField("identity_cards"),$criteria->getSQLOper(), $criteria->getSQLSearchString())
								->order_by($criteria->sidx,$criteria->sord)
								->limit($criteria->limit)
								->offset($criteria->calcOffset())
								->find_all()
								->as_array();
						
			}else{
				$identitycard_list = ORM::factory("dao_identitycard")->order_by($criteria->sidx,$criteria->sord)->limit($criteria->limit)->offset($criteria->calcOffset())->find_all()->as_array(); 
			}
			
		} 

		$dataset = array();
		foreach ($identitycard_list as $identitycard){
			$dataset[$identitycard->id] = array($identitycard->description);
		}
			
		return $dataset;
	}


	/**
		*
		* get all identities card types from an id
		* @param int $minId
		* @param int $maxAmount
		* @return array of identities card types
		*/
	public function getListFromMinID($minId, $maxAmount){
		return ORM::factory("dao_identitycard")->where("id",">",$minId)->order_by("id", "ASC")->limit($maxAmount)->find_all()->as_array();
	}

	public function get($id){
		return ORM::factory("dao_identitycard")->find($id);
	}

	public function getAll(){
		return ORM::factory("dao_identitycard")->find_all()->as_array();
	}

	public function save($id, array $data){
		$this->identitycard = ($id > 0) ? ORM::factory("dao_identitycard",$id) : ORM::factory("dao_identitycard");
		$this->identitycard->description = $data['description'];
		$this->identitycard->save();
		return $this->identitycard->id;
	}

	public function delete($id){
		$identitycard = ORM::factory("dao_identitycard",$id);
		$identitycard->delete();
		return $id;
	}



	/**
		*
		* check if exist an entity identity card with this name
		* @param string $name
		* @return identitycard
		*/
	public function getByName($name){
		return ORM::factory("dao_identitycard")->where("description","=",$name)->find();
	}

}


?>
